#include "net_pre.h"
#include "cmd_line_configer.h"

namespace po = boost::program_options;
namespace Net
{	
	bool CmdLineConfiger::Init(int argc, _TCHAR* argv[])
	{
		po::options_description world_desc("world_desc");
		world_desc.add_options()
		("world", "world_svr_start!")
		("world_ip", po::value<std::string>()->default_value("127.0.0.1"), "world_ip");

		po::options_description zone_desc("zone_desc");
		zone_desc.add_options()
		("zone", "zone_svr_start!")
		("zone_ip", po::value<std::string>()->default_value("127.0.0.1"), "zone_ip");

		po::options_description gate_desc("gate_desc");
		gate_desc.add_options()
		("gate", "gate_svr_start!")
		("gate_ip", po::value<std::string>()->default_value("127.0.0.1"), "gate_ip");

		po::options_description login_desc("login_desc");
		login_desc.add_options()
		("login", "login_svr_start!")
		("login_ip", po::value<std::string>()->default_value("127.0.0.1"), "login_ip");

		po::options_description program_desc;
		program_desc.add(world_desc).add(zone_desc).add(gate_desc).add(login_desc);

		try
		{
			po::store(po::parse_command_line(argc, argv, program_desc), config_vtable_);
			po::notify(config_vtable_);
		}
		catch(...)
		{
			std::cout<<"==========param error!!!=========="<<std::endl;
			std::cout<<program_desc<<std::endl;
			return false;
		}
		return true;
	}

	int CmdLineConfiger::get_int(const char* k)
	{
		if (!config_vtable_.count(k))
		{
			std::cout<<"error!"<<std::endl;
			return 0;
		}
		return config_vtable_[k].as<int>();
	}

	bool CmdLineConfiger::get_bool(const char* k)
	{
		if (!config_vtable_.count(k))
			return false;
		return true;
	}

	float CmdLineConfiger::get_float(const char* k)
	{
		if (!config_vtable_.count(k))
		{
			std::cout<<"error!"<<std::endl;
			return 0.0f;
		}
		return config_vtable_[k].as<float>();
	}

	const char* CmdLineConfiger::get_string(const char* k)
	{
		if (!config_vtable_.count(k))
		{
			std::cout<<"error!"<<std::endl;
			return "";
		}
		return config_vtable_[k].as<std::string>().c_str();
	}
}
